package cc.openkit.admin.controller.api;

import cc.openkit.admin.model.HouseRenovationOffer;
import cc.openkit.admin.service.houseRenovationOffer.HouseRenovationOfferService;
import cc.openkit.admin.util.AppUtil;
import cc.openkit.admin.util.StaticFinalVar;
import cc.openkit.common.KitUtil;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 需求单
 */
@Controller
@Scope("prototype")
@RequestMapping("/hro")
public class HouseRenovationOfferController {

    private Logger log = Logger.getLogger(HouseRenovationOfferController.class);
    private Map<String,Object> map = new HashMap<String, Object>();
    @Resource
    private HouseRenovationOfferService houseRenovationOfferService;
    /**
     * 免费报价 （装修报价预算 免费评估）
     * @return
     */
    @RequestMapping(value = "/addHRO", method = RequestMethod.POST)
    @ResponseBody
    public Object addHRO(HttpServletRequest request) throws Exception{
        log.info("addHRO");
        map = new HashMap<String, Object>();
        Map<String,Object> isNUllMap = new HashMap<String, Object>();
        // 异地登录
        if(!AppUtil.isAppAndLogin(request)){
            return JSONObject.toJSON(KitUtil.returnMap("4001", StaticFinalVar.CHECK_TOKEN));
        }
        HouseRenovationOffer offer = new HouseRenovationOffer();
        String uid = request.getHeader("uid");
        //装修类型 1.装修报价 2.全屋定制 3.无醛设计
        String hroType=request.getParameter("hroType");
        String housePositionId=request.getParameter("housePositionId");
        String housePosition=request.getParameter("housePosition");
        String houseSize=request.getParameter("houseSize");
        String linkPhone=request.getParameter("linkPhone");

        isNUllMap.put(uid,"用户id");
        isNUllMap.put(housePositionId,"请选择地址");
        isNUllMap.put(housePosition,"请选择地址");
        isNUllMap.put(houseSize,"请填写您的房屋面积");
        isNUllMap.put(linkPhone,"请填写您的手机号");
        isNUllMap.put(hroType,"请填写装修类型");
        map=KitUtil.isNull(isNUllMap);
        if (map.get("code").equals("300")){
            return JSONObject.toJSON(map);
        }

        //同一手机号同一种类每天只能限制10次，超出提醒
        String currentDay = KitUtil.dataToString(new Date(), "2");
        Integer count=houseRenovationOfferService.findCountByPhone(linkPhone,currentDay,hroType);
        if (count==10){
            return KitUtil.returnMap("101","您的操作过于频繁，请稍后重试");
        }

        offer.setUserId(uid);
        offer.setHousePositionId(Integer.parseInt(housePositionId));
        offer.setHousePosition(housePosition);
        offer.setHouseSize(Double.parseDouble(houseSize));
        offer.setLinkPhone(linkPhone);
        offer.setHroType(Integer.parseInt(hroType));

        offer.setSubmitTime(new Date());
        offer.setHroId(KitUtil.uuid());

        Integer num =0;
        //1.装修报价 2.全屋定制 3.无醛设计
        if ("1".equals(hroType)){
            //房屋类型（如1室1厅1卫0阳台 存储 1110
            String houseType=request.getParameter("houseType");
            //装修类型 1.新房装修 2.旧房翻新
            String renovationType=request.getParameter("renovationType");

            if (houseType!=null && !"".equals(houseType)){
                offer.setHouseType(houseType);
            }else{
                return KitUtil.returnMap("101","请选择您的房屋户型");
            }

            if (renovationType!=null && !"".equals(renovationType)){
                offer.setRenovationType(Integer.parseInt(renovationType));
            }else{
                return KitUtil.returnMap("101","请选择您的房屋户型");
            }

            num = houseRenovationOfferService.saveSelect(offer);

        }else if ("2".equals(hroType)){
            //家居风格
            String furnitureStyle=request.getParameter("furnitureStyle");
            //房屋类型（如1室1厅1卫0阳台 存储 1110
            String houseType=request.getParameter("houseType");

            if (houseType!=null && !"".equals(houseType)){
                offer.setHouseType(houseType);
            }else{
                return KitUtil.returnMap("101","请选择您的房屋户型");
            }
            if (furnitureStyle!=null && !"".equals(furnitureStyle)){
                offer.setFurnitureStyle(furnitureStyle);
            }else{
                return KitUtil.returnMap("101","请选择定制家居风格");
            }

            num = houseRenovationOfferService.saveSelect(offer);
        }else if ("3".equals(hroType)){
            //备注 选填
            String hroRemark=request.getParameter("hroRemark");
            //称呼
            String hroLinkman=request.getParameter("hroLinkman");
            if (hroRemark!=null && !"".equals(hroRemark)){
                offer.setHroRemark(hroRemark);
            }
            if (hroLinkman!=null && !"".equals(hroLinkman)){
                offer.setHroLinkman(hroLinkman);
            }else{
                return KitUtil.returnMap("101","请输入您的称呼");
            }
            num = houseRenovationOfferService.saveSelect(offer);

        }

        if (num>0){
            map.put("code",200);
            map.put("msg",StaticFinalVar.ADD_OK);
        }else{
            map.put("code",101);
            map.put("msg",StaticFinalVar.ADD_ERR);
        }
        return JSONObject.toJSON(map);
    }

}
